
import { MINO_TYPE } from "./Mino";

export default class SevenBagSystem {

    public static _bag: MINO_TYPE[] = [];
    private static _nextMono: MINO_TYPE = -1;

    static initBagSystem() {
        this._nextMono = this.getOne();
    }

    public static getMino() {
        const mino = this._nextMono;
        this._nextMono = this.getOne();
        return mino;
    }

    public static getNext() {
        return this._nextMono;
    }

    private static fillBag() {
        this._bag = [0, 1, 2, 3, 4, 5, 6];
    }

    private static getOne(): MINO_TYPE {
        if (!this._bag.length) {
            this.fillBag();
        }

        const idx = Math.random() * this._bag.length | 0;
        return this._bag.splice(idx, 1)[0];
    }

    public static test() {
        for (let i = 0; i < 100; i++) {
            const ret = [];
            for (let j = 0; j < 7; j++) {
                ret.push(this.getMino());
            }

            ret.sort((a, b) => a - b);
            console.assert(ret.join('') == '0123456');
        }
    }
}

if (CC_DEBUG) {
    (window as any).SevenBagSystem = SevenBagSystem;
}
